package com.gxpt.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gxpt.app.Constant;
import com.gxpt.app.MessageEvent;
import com.gxpt.app.R;
import com.gxpt.app.base.ListBaseAdapter;
import com.gxpt.app.base.SuperViewHolder;
import com.gxpt.app.bean.getcartinfo_bean;
import com.utils.ScreenUtils;
import com.utils.logger.Logger;
import com.utils.picasso.PicassoUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Lzx on 2016/12/30.
 */

public class shopping_list_Adapter extends ListBaseAdapter<getcartinfo_bean.getcartinfo> {

    //列表图片
    int pic_view_width = 0;
    int pic_view_hight = 0;
    Context context;

    LinearLayout layout_sub;
    CheckBox cb_shop;

    boolean isSub = false;
    public shopping_list_Adapter(Context context) {
        super(context);
        this.context = context;
        int Sviewwidth  = ScreenUtils.getScreenWidth(context);//
        pic_view_width  = Sviewwidth / 4;
        pic_view_hight  = pic_view_width/10*9;

//        Logger.i("pic_view_width = "+pic_view_width);
//        Logger.i("pic_view_hight = "+pic_view_hight);
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_shopping_list;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        getcartinfo_bean.getcartinfo item = mDataList.get(position);

        TextView txt_shop_name = holder.getView(R.id.txt_shop_name);
        txt_shop_name.setText(item.shop_name);

        TextView txt_shop_id = holder.getView(R.id.txt_shop_id);
        txt_shop_id.setText(item.shop_id);

        cb_shop = holder.getView(R.id.cb_shop);

        layout_sub = holder.getView(R.id.layout_sub);
        layout_sub.removeAllViews();
        List<getcartinfo_bean.getcartinfo.good> goods = item.goods;

        if(goods != null ){
            if(goods.size()>0){
                for (int a = 0; a < goods.size(); a++) {
                    layout_sub.addView(creatListItemView(item.goods.get(a), Integer.parseInt(item.goods.get(a).sku_id)));


                }
            }
        }

        cb_shop.setChecked(true);
        cb_shop.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener(){


            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    for(int c=0 ; c < layout_sub.getChildCount() ; c++){
                        CheckBox cb_sku = (CheckBox)layout_sub.getChildAt(c).findViewById(R.id.cb_sku);

                        if(!cb_sku.isChecked()){
                            cb_sku.setChecked(true);
                        }

                    }
                }else{

                    if(isSub){

                    }else{
                        for(int c=0 ; c < layout_sub.getChildCount() ; c++){
                            CheckBox cb_sku = (CheckBox)layout_sub.getChildAt(c).findViewById(R.id.cb_sku);

                            if(cb_sku.isChecked()){
                                cb_sku.setChecked(false);
                            }

                        }
                    }
                }
            }
        });

    }

    private View creatListItemView(getcartinfo_bean.getcartinfo.good d, int id) {
        View convertView = LayoutInflater.from(context).inflate(R.layout.item_item_shopping_list, null);


        //
        JSONObject jsoncar = new JSONObject();
        try {
            jsoncar.put("shop_id", "" + d.shop_id);
            jsoncar.put("cart_id", "" + d.cart_id);
            jsoncar.put("goods_id", "" + d.goods_id);
            jsoncar.put("sku_id", "" + d.sku_id);
            jsoncar.put("goods_num", "" + d.goods_num);
            jsoncar.put("tmp_sell_price", "" + d.sell_price);
        } catch (JSONException e) {
            e.printStackTrace();
        }



        CheckBox cb_sku = (CheckBox)convertView.findViewById(R.id.cb_sku);

        cb_sku.setTag(R.id.sku_info,jsoncar);

        cb_sku.setChecked(true);

        cb_sku.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener(){


            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {



                if(isChecked){
                    EventBus.getDefault().post(new MessageEvent(Constant.ACTION_carinfo_add,buttonView.getTag(R.id.sku_info).toString(),"",""));

                    boolean isall = true;
                    for(int c=0 ; c < layout_sub.getChildCount() ; c++){
                        CheckBox cb_sku = (CheckBox)layout_sub.getChildAt(c).findViewById(R.id.cb_sku);

                        if(cb_sku.isChecked()){
                            isall = true;

                        }else{
                            isall = false;
                            break;
                        }
                    }
                    if(isall){
                        isSub = false;
                        cb_shop.setChecked(true);
                    }
                }else{
                    EventBus.getDefault().post(new MessageEvent(Constant.ACTION_carinfo_sub,buttonView.getTag(R.id.sku_info).toString(),"",""));

                    isSub = true;
                    cb_shop.setChecked(false);
//                    EventBus.getDefault().post(new MessageEvent(Constant.ACTION_to_shopping_allCheck,false));
                }
            }
        });



        ImageView img_thumb= (ImageView)convertView.findViewById(R.id.img_thumb);
        if(d.thumb.isEmpty()){
            PicassoUtils.Picasso_displayImage(context,R.mipmap.ic_launcher,pic_view_width,pic_view_hight,img_thumb);
        }else{
            PicassoUtils.Picasso_displayImage(context, Constant.IP +d.thumb,pic_view_width,pic_view_hight,img_thumb);
        }

        TextView txt_goods_name = (TextView)convertView.findViewById(R.id.txt_goods_name);
        txt_goods_name.setText(d.goods_name);

        TextView txt_sku_name = (TextView)convertView.findViewById(R.id.txt_sku_name);
        txt_sku_name.setText(d.sku_name);

        TextView txt_sell_price = (TextView)convertView.findViewById(R.id.txt_sell_price);
        txt_sell_price.append(d.sell_price);

        TextView txt_sku_id = (TextView)convertView.findViewById(R.id.txt_sku_id);
        txt_sku_id.setText(d.sku_id);

        TextView txt_goods_id = (TextView)convertView.findViewById(R.id.txt_goods_id);
        txt_goods_id.setText(d.goods_id);

        final TextView txt_cart_id = (TextView)convertView.findViewById(R.id.txt_cart_id);
        txt_cart_id.setText(d.cart_id);


        TextView txt_buy_num = (TextView)convertView.findViewById(R.id.txt_buy_num);
        txt_buy_num.setText(d.goods_num);

        TextView txt_jian = (TextView)convertView.findViewById(R.id.txt_jian);
        TextView txt_add  = (TextView)convertView.findViewById(R.id.txt_add);


        txt_jian.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEvent(
                        Constant.ACTION_to_shopping_add,txt_cart_id.getText().toString(),"reduce"));

            }
        });

        txt_add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEvent(
                        Constant.ACTION_to_shopping_reduce,txt_cart_id.getText().toString(),"add"));
            }
        });

        convertView.setId(id);
        return convertView;
    }
}
